package com.asana.resteasy.api.client;


import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.client.core.executors.ApacheHttpClientExecutor;
import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
import org.jboss.resteasy.spi.ResteasyProviderFactory;

public class RestClientFactory {
	
	public static AsanaRestInterface connect(String key){
		String url = "https://app.asana.com/";
		
		RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
		Credentials credentials = new UsernamePasswordCredentials(key);

		HttpClient httpClient = new HttpClient();	
		httpClient.getState().setCredentials(AuthScope.ANY, credentials);
		httpClient.getParams().setAuthenticationPreemptive(true);
		   
		ApacheHttpClientExecutor executor = new ApacheHttpClientExecutor(httpClient) {
		    
			@Override
		    public ClientResponse execute(ClientRequest request) throws Exception {
		        return super.execute(request);
		    }           
		};
		
		AsanaRestInterface client = ProxyFactory.create(AsanaRestInterface.class, url, executor);
		return client;
		
	}
	
	

}
